package com.myapp.ruyixing.ui;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.model.ShopAlbum.PhotoItem;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;

public class ShopBigAlbumActivity extends BaseActivity {

	private String mShopId;
	private String mShopName;
	private ArrayList<PhotoItem> mData;
	private int mPosition;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_big_album);

		mShopId = getIntent().getStringExtra(ShopDetailActivity.INTENT_SHOP_ID);
		mShopName = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_NAME);
		mData = (ArrayList<PhotoItem>) getIntent().getSerializableExtra(
				"photos");
		mPosition = getIntent().getIntExtra("position", 0);

		if (TextUtils.isEmpty(mShopName) == false) {
			((TitleBar) findViewById(R.id.title_bar)).setTitleText("图片详情\n"
					+ mShopName);
		}

		initView();
	}

	private void initView() {
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
		vp.setPageMargin(80);
		vp.setAdapter(new ImageFragmentPagerAdapter(getSupportFragmentManager()));
		vp.setCurrentItem(mPosition);
	}

	/**
	 * Image pager adapter which uses fragment as a container.
	 * 
	 * @author laiwenjie
	 * @date 2012-9-10
	 * 
	 */
	private class ImageFragmentPagerAdapter extends FragmentStatePagerAdapter {

		public ImageFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Fragment getItem(int position) {
			return ImageFragment.newInstance(mData.get(position));
		}
	}

	/**
	 * Detail image fragment
	 * 
	 * @author laiwenjie
	 * @date 2012-9-10
	 * 
	 */
	public static class ImageFragment extends Fragment {
		public static final String IMAGE_DATA = "image_data";

		private PhotoItem data;

		public static ImageFragment newInstance(PhotoItem data) {
			ImageFragment fragment = new ImageFragment();

			Bundle b = new Bundle();
			b.putSerializable(IMAGE_DATA, data);

			fragment.setArguments(b);

			return fragment;
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			if (getArguments() != null) {
				data = (PhotoItem) getArguments().getSerializable(IMAGE_DATA);
			}
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			return inflater.inflate(R.layout.cell_shop_big_album, container,
					false);
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);

			View view = getView();
			((TextView) view.findViewById(R.id.text_name)).setText(data.title);
			((TextView) view.findViewById(R.id.date)).setText(data.datetime);
			((TextView) view.findViewById(R.id.user)).setText(data.username);
			int level = 0;
			try {
				level = Integer.parseInt(data.score);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (level > 0) {
				((ImageView) view.findViewById(R.id.score))
						.setImageLevel(level);
			} else {
				((ImageView) view.findViewById(R.id.score))
						.setVisibility(View.GONE);
			}

			RequestQueue mQueue = Volley.newRequestQueue(getActivity());
			ImageLoader mImageLoader = new ImageLoader(mQueue,
					new BitmapLruCache());
			if (!TextUtils.isEmpty(data.picurl)) {
				((NetworkImageView) view.findViewById(R.id.picture))
						.setImageUrl(data.picurl, mImageLoader);
			}
		}

		@Override
		public void onDestroyView() {
			super.onDestroyView();
		}
	}
}
